package com.comdo.newup_loan.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.comdo.newup_loan.comom.ProgressDialog;
import com.comdo.newup_loan.util.CommonUtil;
import com.comdo.newup_loan.util.StringUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;



public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView{

    private Unbinder unbinder;
    protected P mPresenter;
    /**
     * 获取布局ID
     */
    protected abstract int getContentViewLayoutID();

    /**
     * 界面初始化
     */
    protected abstract void init();
    /**
     * 初始化布局以及View控件
     */
    protected abstract void initView(Bundle savedInstanceState);
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getContentViewLayoutID() != 0) {
            return inflater.inflate(getContentViewLayoutID(), container, false);
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        mPresenter = getmPresenter();
        presenterSetView();
        initView(savedInstanceState);
        init();
    }

    /**
     * 获取presenter实例
     * @return presenter实例
     */
    public abstract P getmPresenter();

    /**
     * 设置presenter持有view引用
     */
    public abstract void presenterSetView();
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(unbinder != null)
            unbinder.unbind();
        if(mPresenter!=null)
            mPresenter.removeView();

    }
    private Toast mToast;
    @Override
    public void setTargetFragment(Fragment fragment, int requestCode) {
        super.setTargetFragment(fragment, requestCode);
    }
    protected void showToast(String desc){
        if (mToast == null){
            mToast = Toast.makeText(this.getActivity().getApplicationContext(),desc,Toast.LENGTH_SHORT);
        }else{
            mToast.setText(desc);
        }
        mToast.show();
    }

    @Override
    public void showLoadingDialog(String msg) {
        if(StringUtils.isNull(msg)){
            ProgressDialog.show(getActivity(),false,"加载中");
        }else{
            ProgressDialog.show(getActivity(),false,msg);
        }
    }

    @Override
    public void dismissLoadingDialog() {

    }
    @Override
    public void exitLogin() {
        CommonUtil.exitLogin(getActivity());
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }
}
